package com.ironman.compute;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ironman.common.entity.HeartrateData;
import com.ironman.common.OriginalSimpleData;
import com.ironman.event.topic.dami.OriginalTopic;
import com.ironman.common.util.AccuracyUtil;
import lombok.extern.slf4j.Slf4j;
import org.noear.dami.Dami;

import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;

/**
 * 心率计算
 */
@Slf4j
public class HeartbeatAverageComputer extends BaseAverageComputer<HeartrateData> {

    public HeartbeatAverageComputer(Integer windowSize) {
        super(windowSize);
    }

    public void init(int idx, BiConsumer<Date, Double> heartrateConsumer) {
        Dami.<OriginalSimpleData, Void>bus().listen(OriginalTopic.ORIGINAL_TOPIC + HEARTRATE, payload -> {
            OriginalSimpleData originalSimpleData = payload.getContent();
            Date ts = originalSimpleData.getTs();
            String data = originalSimpleData.getData();
            String[] split = data.split("\\|");
            computeWindowData(new HeartrateData(ts, Integer.parseInt(split[idx])), heartrateConsumer);
        });
    }

    public void computeWindowData(HeartrateData heartrateData, BiConsumer<Date, Double> heartrateConsumer) {
        windowData(heartrateData, heartrateConsumer);
    }

    public static void main(String[] args) {
        HeartbeatAverageComputer heartbeatComputer = new HeartbeatAverageComputer(1);

        List<HeartrateData> heartrateDataList = ListUtil.of(
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:00", DatePattern.NORM_DATETIME_PATTERN), 112),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:01", DatePattern.NORM_DATETIME_PATTERN), 120),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:02", DatePattern.NORM_DATETIME_PATTERN), 148),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:03", DatePattern.NORM_DATETIME_PATTERN), 268),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:04", DatePattern.NORM_DATETIME_PATTERN), 59),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:05", DatePattern.NORM_DATETIME_PATTERN), 67),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:06", DatePattern.NORM_DATETIME_PATTERN), 11),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:07", DatePattern.NORM_DATETIME_PATTERN), 114),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:08", DatePattern.NORM_DATETIME_PATTERN), 88),
                new HeartrateData(DateUtil.parse("2024-12-15 12:00:09", DatePattern.NORM_DATETIME_PATTERN), 100)
        );
        for (HeartrateData headingData : heartrateDataList) {
            heartbeatComputer.computeWindowData(headingData, new BiConsumer<Date, Double>() {
                @Override
                public void accept(Date ts, Double average) {
                    log.info("{}=>当前平均心率{}bpm", ts, AccuracyUtil.downHeartrateInteger(average));
                }
            });
        }
    }
}
